<?php 
class kdtrace_rpxex extends Controller {
	
	function track( $tracknum  , $lang = "en"){
		$ch = curl_init();
        curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
		if($lang=="ch"){
			$langset = "ch";
			$url 	 = "http://tracking.rpx.com.cn/?ShipmentList=".$tracknum."" ;
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Referer: http://tracking.rpx.com.cn/',
				'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
			));
			
		}else{
			$langset = "en";
			$url 	 = "http://www.rpxonline.com/index.php?option=com_track&view=tracking" ;
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_POST, 1);
			curl_setopt($ch, CURLOPT_POSTFIELDS, "qType=T&sid=" . $tracknum);
			curl_setopt($ch, CURLOPT_HTTPHEADER, array(
				'Content-Type: application/x-www-form-urlencoded',
				'Referer: http://www.rpxonline.com/',
				'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/33.0.1750.154 Safari/537.36' 
			));
		}
		

		$result = curl_exec($ch);
        curl_close($ch);
		$rtndata = array('tracknum'=>$tracknum,'header'=>array(),'err'=>array(),'eventList'=>array());
		$result = $this->skipSpaces($result);

		if($result==""){
			$rtndata['err']['errcode'] = '2';
			return $rtndata;
			
		}
		if(strpos($result , "No such shipment </div>")>0 || strpos($result , "此单号没有数据，请确认您的单号是否正确。")>0 ){
			$rtndata['err']['errcode'] = '1';
			return $rtndata;
		}
		$rtndata['err']['errcode'] = '0';
		

		$rhtm = str_replace("&nbsp;" , " " , $result);
		if($langset == "ch"){
			if(preg_match( "/<span id\=\"lblConsigneeCountry\">(.+?)<\/span>/i" , $rhtm, $matches) == 1){
				$rtndata['header']['shipTo']		= $matches[1];
			};
			if(preg_match( "/<span id\=\"lblConsigneeCity\">(.+?)<\/span>/i" , $rhtm, $matches) == 1){
				$rtndata['header']['recipientCity']		= $matches[1];
			};
		}else{
			if(preg_match( "/<div class\=\"hemisphere\">Destination:<\/div> <div class\=\"hemisphere\">(.+?)<\/div>/i" , $rhtm, $matches) == 1){
				$rtndata['header']['shipTo']		= $matches[1];
			};
			if(preg_match( "/<div class\=\"hemisphere\">Origin:<\/div> <div class\=\"hemisphere\">(.+?)<\/div>/i" , $rhtm, $matches) == 1){
				$rtndata['header']['shipFrom']		= $matches[1];
			};
		}
		if($langset == "ch"){
			
			$w1 = strpos($result , 'id="gvShipmentTrackingList"' );
			$w2 = strpos($result , '</table>' ,$w1);

		}else{
			$w1 = strpos($result , "<h2>Shipment Travel History</h2>" );
			$w1 = strpos($result , '<div class="row row1">' , $w1 );
			$w2 = strpos($result , '<div class="footer">' ,$w1);
		}
		$bdhtm = substr($result , $w1 , $w2 - $w1);
		$bdhtm = str_replace("&nbsp;" , " " , $bdhtm);
		if($langset == "ch"){
			preg_match_all("/<td style\=\"width:120px;\">(.+?)<\/td><td align\=\"left\" style\=\"width:80px;\">(.+?)<\/td><td align\=\"center\" style\=\"width:80px;\"> <span id\=\"gvShipmentTrackingList_ct(?:.+?)_lblEventCode\">(.+?)<\/span> <\/td><td align\=\"left\" style\=\"width:120px;\">(.+?)<\/td><td align\=\"left\">(.+?)<\/td>/i", $bdhtm, $matches);
		}
		if($langset == "en"){
			preg_match_all("/<div class\=\"trace_column tc_date\">(.+?)<\/div> <div class\=\"trace_column tc_activity\">(.+?)<\/div> <div class\=\"trace_column tc_location\">(.+?)<\/div> <div class\=\"trace_column tc_comment\">(.+?)<\/div>/i", $bdhtm, $matches);
		}
		
		$count = count($matches[0]);
		if($count>0){
			for($i=0 ; $i < $count ; $i++){
				//$this->skipSpaces($matches[1][$i])
				if($langset == "ch"){
					$tt =  $this->str_format_time( trim($matches[1][$i]) );
					$rtndata['eventList'][$i] = array(
						"date"			=> date('Y-m-d',$tt),
						"time"			=> date('h:i:s',$tt),
						"utime"			=> $tt,
						"strtime"		=>trim($matches[1][$i]),
						"status"		=> trim($matches[4][$i]),
						"location"		=> trim($matches[2][$i])
					);
				}else{
					$tt =  strtotime( trim($matches[1][$i]) );
					$rtndata['eventList'][$i] = array(
						"date"			=> date('Y-m-d',$tt),
						"time"			=> date('h:i:s',$tt),
						"utime"			=> $tt,
						"status"		=> trim($matches[2][$i]) . " - " . trim($matches[4][$i]),
						"location"		=> trim($matches[3][$i])
					);
				}
				

			}
		}

		$rtndata['eventList'] = array_reverse( $rtndata['eventList'] );
		return $rtndata;
	}
	function skipSpaces($t){
			return  trim(preg_replace('/\s{2,}/', ' ', $t));
	}
	
	function str_format_time($times){
		if(preg_match("/^(.+?)\/(.+?)\/(.+?) (.+?):(.+?)$/i" , $times , $matchs)){
			$timestamp = mktime($matchs[4] , $matchs[5] , 0, $matchs[2], $matchs[1], $matchs[3]);
		}
		return $timestamp;
	}
}
?>